package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量导入预约设置数据
    @Override
    public void addList(List<List<String>> list) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(list != null && list.size()>1){
            //遍历
            for (int i=1;i<list.size();i++) {
                List<String> rows = list.get(i);
                String orderDate = rows.get(0);
                String number = rows.get(1);
                if(orderDate != null && orderDate != "") { //判断日期非空
                    //判断日期是否已存在
                    int num = orderSettingDao.getOrderDateNum(orderDate);
                    if(num == 0){
                        //设置orderSetting
                        OrderSetting orderSetting = new OrderSetting();
                        orderSetting.setOrderDate(sdf.parse(orderDate));
                        orderSetting.setNumber(Integer.valueOf(number));
                        orderSetting.setReservations(0);
                        //保存到数据库
                        orderSettingDao.save(orderSetting);
                    }
                }
            }
        }
    }

    /**
     * 任务5-根据月份查询数据
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<OrderSetting> getOrderSettingByMonth(int year, int month) {

        //构造日期范围
        String startDate = String.format("%d-%d-1", year, month);
        String endDate = String.format("%d-%d-31", year, month);
        //DAO查询并返回
        return orderSettingDao.selectByOrderDateRange(startDate, endDate);
    }
    /**
     * //任务6-添加预约设置(每日可预约人数和已经预约的人数)
     * @param orderSetting
     */
    @Override
    public void addTimeAndNumber(OrderSetting orderSetting) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date orderDate = orderSetting.getOrderDate();
//        String form = sdf.format(orderDate);
//        Date date = null;
//        try {
//            date = sdf.parse(form);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        //获取前端的日期2022年9月8日
        Date orderDate = orderSetting.getOrderDate();
        //年-月-日格式 2022-09-08
        String time = String.format("%tF", orderDate);
        //String转换成date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //设置时间
        orderSetting.setOrderDate(date);
        //获取到前端的可预约人数
        int number = orderSetting.getNumber();
        //获取到前端的已预约人数
        int reservations = orderSetting.getReservations();
        if (reservations <= number){
            orderSettingDao.insertTimeAndNumber(orderSetting);
        }else{
            throw new BusinessRuntimeException("预约的人数已满");
        }
    }
}
